<?php

namespace app\admin\controller;

use app\admin\logic\WorkRecordAccountLogic;

/**
 * 日记账明细api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/21
 * Time: 09:21
 */
class WorkRecordAccount extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->wra_logic = new WorkRecordAccountLogic();
    }

    /**
     * 获取日记账明细
     * @return \think\response\Json
     */
    public function ajax_get_wra()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //获取日记账明细
        $r = $this->wra_logic->get_wra($params);

        $data['list'] = $r['data']['list'];
        $data['total_num'] = $r['data']['total_num'];
        return json($this->wra_logic->get_response(1, 'OK', $data));
    }

    /**
     * 获取日记账明细详情
     * @return \think\response\Json
     */
    public function ajax_get_wra_detail()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = [];
        $maps[] = ['id', '=', $params['wraid']];
        $wra_detail =  model('work_record_account')->get_detail($maps, 'id,openid,department,now_date,receipt_number,type,abstract,contract_number,income_and_expend,money');
        $wra_detail['money'] = deal_number_div_100($wra_detail['money']);

        $data['wra_detail'] = $wra_detail;
        return json($this->wra_logic->get_response(1, 'OK', $data));
    }

    /**
     * 修改日记账明细
     * @return \think\response\Json
     */
    public function ajax_save_wra()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查修改日记账明细规则
        $r = $this->wra_logic->check_save_wra_rule($params);
        if (!$r['code']) {
            return json($r);
        }
        $wra_detail = $r['data']['wra_detail'];

        $wca_data = [];
        $wca_data['department'] = $params['department'];
        $wca_data['now_date'] = $params['now_date'];
        $wca_data['receipt_number'] = trim($params['receipt_number']);
        $wca_data['type'] = $params['type'];
        $wca_data['abstract'] = trim($params['abstract']);
        $wca_data['contract_number'] = trim($params['contract_number']);
        $wca_data['income_and_expend'] = $params['income_and_expend'];
        $wca_data['money'] = deal_number_mul_100(trim($params['money']));

        $maps = [];
        $maps[] = ['id', '=', $wra_detail['id']];
        $r = model('work_record_account')->force()->save($wca_data, $maps);
        if ($r !== false) {
            return json($this->wra_logic->get_response(1, '修改成功'));
        }
        return json($this->wra_logic->get_response(0, '修改失败'));
    }

    /**
     * 获取日记账配置
     * @return \think\response\Json
     */
    public function ajax_get_account_conf()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = [];
        $maps[] = ['key', '=', 'work_recoud_account_type'];
        $conf_info = $this->wra_logic->get_conf_info($maps);
        $work_recoud_account_type = !empty($conf_info['work_recoud_account_type']) ? json_decode($conf_info['work_recoud_account_type'], true) : [];

        $data['work_recoud_account_type'] = $work_recoud_account_type;
        return json($this->wra_logic->get_response(1, 'OK', $data));
    }

    /**
     * 删除日记账明细
     * @return \think\response\Json
     */
    public function ajax_del_wra()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $r = model('work_record_account')->destroy($params['wraid']);
        if ($r) {
            return json($this->wra_logic->get_response(1, '删除成功'));
        }
        return json($this->wra_logic->get_response(0, '删除失败'));
    }

    /**
     * 导出日记账明细
     * http://127.0.0.1/mandiao-mini-api/public/admin-work_record_account-export_wra?token=e916a6039bff505bacaf5def2bb8435f&page=1&rows=10&department=全部&username=&type=全部&income_and_expend=-1&start_time=&end_time=
     * @return \think\response\Json
     */
    public function export_wra()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //获取日记账明细
        $r = $this->wra_logic->get_wra($params);
        $list = $r['data']['list'];

        $row = ['部门名称', '提交人', '收据编号', '类别', '摘要', '合同编号', '收/支', '金额（元）', '日期'];

        $export_data = [];
        foreach ($list as $k => $v) {
            $export_data[$k]['department'] = $v['department'];
            $export_data[$k]['username'] = $v['username'];
            $export_data[$k]['receipt_number'] = $v['receipt_number'];
            $export_data[$k]['type'] = $v['type'];
            $export_data[$k]['abstract'] = $v['abstract'];
            $export_data[$k]['contract_number'] = $v['contract_number'];
            $export_data[$k]['income_and_expend_zw'] = $v['income_and_expend_zw'];
            $export_data[$k]['money'] = $v['money'];
            $export_data[$k]['now_date'] = $v['now_date'];
        }
        array_unshift($export_data, $row);

        $file_name = '日记账明细' . date('YmdHi') . '.xlsx';
        $r = $this->wra_logic->gen_wra_excel($file_name, $export_data);

        //$this->redirect(\think\facade\Request::root(true) . '/' . $file_name);
        return json($this->wra_logic->get_response(1, '导出成功'));
    }
}
